package com.jk.web.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.jk.common.model.OrderBean;
import com.jk.web.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("pay")
public class PayController {

    @Autowired
    private OrderService  orderService;

    private final String APP_ID = "2016101200668505";
    private final String APP_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCXbT0GTP5vW5heznwGhycKiD8HLiuTrmtocfeH1AAjv5W351AaJ58y9iBxCtQUXmklNC1oDIjc5kPjpR5g/Wdv7DQa+t8FKTh5R+DKipRhJo7LjZDVdBfKUQtP24OzibZQV0+1DvnaIsy5t+8vWYFprHgzL2JYTTOX65iN8edzfObaDzmm9a/9ckE+0B8+PTEAc4Dglduwu3gzwAC4Er2//s5oGD/NKA2AES56tofKXW7dyMoHI3uADEDUWcIkOgnZeOptHwAImYIEptXGiubjNPtZ1n3VZCYdprln+Z2YmCulfHCdvx3S2/tuy/eqg/UWqRBCw2nbul/PAuKWaF/PAgMBAAECggEAW1HqonkeXEPN0ScPTtSE6+ocsJTcfNSMO9G3KLfmWai5fs3F1fAdJxudlu6j1aDWIjSkSYJ9q1JGkOvMNUu87JdzGO5APxmL0EBTxXHrZ+db6E/4j3HSwxU7EC7e7xBW3X66OAPPFAk2k63kVhbRX8w+NQgXdA8i1TSkrL29CjzRYSv9aJLmtC9xWR5XbXjC2+GRm/STomfGmnAM11VyJlokXmSqo23Rd/bWKVad0bSoVGsGuCLicLo3MOlCL4q40VL+B5hsahkSyuSnsWGv3zSDahLM/4EgncC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QrUFF5pJTQtaAyI3OZD46UeYP1nb+w0GvrfBSk4eUfgyoqUYSaOy42Q1XQXylELT9uDs4m2UFdPtQ752iLMubfvL1mBaax4My9iWE0zl+uYjfHnc3zm2g85pvWv/XJBPtAfPj0xAHOA4JXbsLt4M8AAuBK9v/7OaBg/zSgNgBEueraHyl1u3cjKByN7gAxA1FnCJDoJ2XjqbR8ACJmCBKbVxorm4zT7WdZ91WQmHaa5Z/mdmJgrpXxwnb8d0tv7bsv3qoP1FqkQQsNp27pfzwLilmhfzwIDAQAB";
    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";

    String  a="";

    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "http://127.0.0.1/notify";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private  final String RETURN_URL = "http://127.0.0.1:8764/messager/saveOrder?orderNumber=";

    @RequestMapping("alipay")
    public  void   alipay(String orderNumber,HttpServletResponse httpResponse) throws IOException {


        //根据订单号查询数据
        OrderBean orderBean= orderService.orderNumber(orderNumber);
        //Integer flowerPrice = orderBean.getFlowerPrice();   //价格
        //String flowerColorName = orderBean.getFlowerColorName();//鲜花名字

        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //在公共参数中设置回跳和通知地址
        request.setReturnUrl(RETURN_URL+orderNumber);

        request.setNotifyUrl(NOTIFY_URL);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        //生成随机Id
        /*String out_trade_no = UUID.randomUUID().toString();*/
        //付款金额，必填
        String total_amount =Integer.toString(999);  //flowerPrice
        //订单名称，必填
        String subject ="flowerColorName";  //flowerColorName
        //商品描述，可空
        //  String body = "";
        request.setBizContent("{\"out_trade_no\":\""+ orderNumber +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
              //  + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String form = "";
        try {
            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();


        //用回调函数名称包裹返回数据，这样，返回数据就作为回调函数的参数传回去
       // return form;
    }



}

